package com.qf.springbootproject.controller;

import com.qf.springbootproject.ResultBean.JsonResult;
import com.qf.springbootproject.exception.BusinessErrorException;
import com.qf.springbootproject.exception.BusinessMsgEnum;
import com.qf.springbootproject.pojo.User;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/exception")
@Slf4j
@Api(tags = "测试异常接口文档")
public class EXceptionController {


    /**
     * 测试参数为空异常
     * @param id
     * @param name
     * @return
     */
    @RequestMapping("/test1")
    @ResponseBody
    public String test1(@RequestParam String id,@RequestParam String name){
        System.err.println(id);
        System.err.println(name);
        // 测试参数为空异常
        log.info("name：{}", name);
        log.info("id：{}", id);
        return name;
    }

    /**
     * 测试空指针异常
     * @param user
     * @return
     */
    @RequestMapping("/test2")
    @ResponseBody
    public JsonResult test2(User user){
        System.out.println(user.getUserName().toString());
        log.info("name：{}", user);
        JsonResult result = JsonResult.resultOk(user);

        return result;
    }

    /**
     * 测试全局异常Exception
     * @param user
     * @return
     */
    @RequestMapping("/test3")
    @ResponseBody
    public JsonResult test3(User user){

        int i = 1/0;
        log.info("name：{}", user);
        JsonResult result = JsonResult.resultOk(user);
        return result;
    }

    /**
     * 测试自定义异常
     * @param user
     * @return
     */
    @RequestMapping("/test4")
    @ResponseBody
    public JsonResult test4(User user){

        try {
            int i = 1 / 0;
        } catch (Exception e) {
            throw new BusinessErrorException(BusinessMsgEnum.UNEXPECTED_EXCEPTION);
        }

        log.info("name：{}", user);
        JsonResult result = JsonResult.resultOk(user);
        return result;
    }

    /**
     * 测试环绕通知
     * @param user
     * @return
     */
    @RequestMapping("/test5")
    @ResponseBody
    public User test5(User user){


        User result = new User("1", "zs", "123");

        return result;
    }

}
